import router from "@/router/index";
import nProgress from "nprogress";
import "nprogress/nprogress.css";
import store from "@/store/index";

// 白名单不需要鉴权
let whiteList = ['/login','404']

router.beforeEach(async (to, from, next) => {
  nProgress.start() 
  // 有token
  if(store.getters.token) {
    if(to.path === '/login') {
      next({path: '/'})
      nProgress.done()
    }else {
      // 判断是否获取过用户的个人信息
      if(!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  }else {
    if(whiteList.indexOf(to.path) > -1){
      next()
    } else {
      next({path:'/login'})
      nProgress.done()
    }
  } 
})


router.afterEach((to, from) => {
  nProgress.done()
})

